﻿using System;
using PdfSdk.PdfViewer;
using Syncfusion.Pdf.Parsing;

namespace PdfSdk.Pdf
{
    public class PdfCrossTableWrapper : ObjectWrapper
    {
        public static readonly Type OriginalType = Type.GetType($"Syncfusion.Pdf.IO.PdfCrossTable, {typeof(PdfLoadedDocument).Assembly.FullName}");

        public PdfCrossTableWrapper(object obj) : base(obj)
        {
        }

        public static IPdfPrimitiveWrapper Dereference(IPdfPrimitiveWrapper obj)
        {
            return obj is PdfReferenceHolderWrapper pdfReferenceHolder ? pdfReferenceHolder.Object : obj;
        }

        public IPdfPrimitiveWrapper GetObject(IPdfPrimitiveWrapper pointer)
        {
            if (pointer == null)
                return null;

            var obj = InvokeMethod("GetObject", pointer.OriginalObject);
            return PdfPrimitiveHelper.GetPrimitiveWrapper(obj);
        }

        public PdfReferenceWrapper GetReference(IPdfPrimitiveWrapper obj)
        {
            if (obj == null)
                return null;

            var result = InvokeMethod("GetReference", new Type[] { PdfTypes.IPdfPrimitive }, obj.OriginalObject);
            if (result == null)
                return null;
            return new PdfReferenceWrapper(result);
        }
    }
}
